package com.wtwd.wtwddevice.service.impl;


import com.wtwd.device.util.DateUtils;
import com.wtwd.wtwddevice.mapper.DeviceBloodPressureMapper;
import com.wtwd.wtwddevice.model.entity.DeviceBloodOxygenTotal;
import com.wtwd.wtwddevice.model.entity.DeviceBloodPressure;
import com.wtwd.wtwddevice.model.entity.DeviceBloodPressureTotal;
import com.wtwd.wtwddevice.service.IDeviceBloodPressureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.wtwddevice.service.IDeviceBloodPressureTotalService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2024-05-29
 */
@Service
public class DeviceBloodPressureServiceImpl extends ServiceImpl<DeviceBloodPressureMapper, DeviceBloodPressure> implements IDeviceBloodPressureService {


    @Resource
    private IDeviceBloodPressureTotalService deviceBloodPressureTotalService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insert(DeviceBloodPressure deviceBloodPressure) {
        DeviceBloodPressureTotal deviceBloodPressureTotal = getSynchronizationCache(deviceBloodPressure);
        deviceBloodPressure.setDeviceBloodPressureTotalId(deviceBloodPressureTotal.getId());
        boolean save = save(deviceBloodPressure);
        if (save){
            deviceBloodPressureTotal.setLastInfoId(deviceBloodPressure.getId());
            deviceBloodPressureTotalService.insertOrUpdate(deviceBloodPressureTotal);
        }
        return save;
    }

    @Override
    public DeviceBloodPressureTotal getSynchronizationCache(DeviceBloodPressure deviceBloodPressure) {
        DeviceBloodPressureTotal deviceBloodPressureTotal = deviceBloodPressureTotalService.getDeviceBloodPressureTotal(deviceBloodPressure.getDeviceId(), DateUtils.convertDateFormat(deviceBloodPressure.getTime()));
        if (deviceBloodPressureTotal != null) {
            deviceBloodPressureTotal.setMin(deviceBloodPressureTotal.getMin() > deviceBloodPressure.getValue() ? deviceBloodPressure.getValue() : deviceBloodPressureTotal.getMin());
            deviceBloodPressureTotal.setMax(deviceBloodPressureTotal.getMax() < deviceBloodPressure.getValue() ? deviceBloodPressure.getValue() : deviceBloodPressureTotal.getMax());
            deviceBloodPressureTotal.setTotal(deviceBloodPressureTotal.getTotal() == null ? deviceBloodPressure.getValue() : (deviceBloodPressureTotal.getTotal() + deviceBloodPressure.getValue()));
            deviceBloodPressureTotal.setRows(deviceBloodPressureTotal.getRows() == null ? 1 : deviceBloodPressureTotal.getRows() + 1);
        } else {
            deviceBloodPressureTotal = new DeviceBloodPressureTotal();
            BeanUtils.copyProperties(deviceBloodPressure, deviceBloodPressureTotal);
            deviceBloodPressureTotal.setMin(deviceBloodPressure.getValue());
            deviceBloodPressureTotal.setMax(deviceBloodPressure.getValue());
            deviceBloodPressureTotal.setTotal(deviceBloodPressure.getValue());
            deviceBloodPressureTotal.setRows(1);
            deviceBloodPressureTotal.setTime(DateUtils.convertDateFormat(deviceBloodPressure.getTime()));
        }
        return deviceBloodPressureTotal;
    }
}
